package com.example.yyh.recorddemo.adapter;

import android.content.Context;
//import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;

import com.example.yyh.recorddemo.R;
import com.example.yyh.recorddemo.base.AbstractAdapter;
import com.example.yyh.recorddemo.bean.SoundBean;
import com.example.yyh.recorddemo.databinding.ItemRecordListLayoutBinding;

/**
 * Created by yaoyihang on 2018/12/1.
 */

public class RecordListAdapter extends AbstractAdapter<SoundBean> {


    /**
     * 定义一个接口，回调点击事件
     */
    public interface OnClickListener {
        void play(SoundBean soundBean, int position);

        void more(SoundBean soundBean, int position);
    }

    private OnClickListener mListener;

    /**
     * 设置接口对象
     *
     * @param lstener
     */
    public void setListener(OnClickListener lstener) {
        this.mListener = lstener;
    }

    public RecordListAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            ItemRecordListLayoutBinding binding = DataBindingUtil.inflate(mInflater, R.layout.item_record_list_layout, null, false);
            viewHolder = new ViewHolder(binding);
            convertView = binding.getRoot();
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final SoundBean item = getItem(position);
        viewHolder.binding.setSoundBean(getItem(position));
        viewHolder.binding.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.more:
                        mListener.more(item, position);
                        break;
                    case R.id.ll_item:
                        mListener.play(item, position);
                        break;
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        ItemRecordListLayoutBinding binding;

        public ViewHolder(ItemRecordListLayoutBinding binding) {
            this.binding = binding;
        }

        public ItemRecordListLayoutBinding getBinding() {
            return binding;
        }

        public void setBinding(ItemRecordListLayoutBinding binding) {
            this.binding = binding;
        }
    }
}
